package jp.ac.u_tokyo.dch.view.conv;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import jp.ac.u_tokyo.dch.model.output.Attribute;
import jp.ac.u_tokyo.dch.model.output.XmlComposite;
import jp.ac.u_tokyo.dch.model.output.XmlData;
import jp.ac.u_tokyo.dch.util.common.XmlOutputUtil;

public class ConvView {

	private List<XmlData> xmlObjList;

	private String targetTagId;

	private String delAttrIndex;

	private String keySchemaid;

	private String displayTagId;

	private String insertType;

	private String sort;


	public ConvView() {
		xmlObjList = new ArrayList<XmlData>();
	}

	public List<XmlData> getXmlObjList() {
		return xmlObjList;
	}

	public void setXmlObjList(List<XmlData> xmlObjList) {
		this.xmlObjList = xmlObjList;
	}

	public String getTargetTagId() {
		return targetTagId;
	}

	public void setTargetTagId(String targetTagId) {
		this.targetTagId = targetTagId;
	}

	public String getKeySchemaid() {
		return keySchemaid;
	}

	public void setKeySchemaid(String keySchemaid) {
		this.keySchemaid = keySchemaid;
	}

	public String getDelAttrIndex() {
		return delAttrIndex;
	}

	public void setDelAttrIndex(String delAttrIndex) {
		this.delAttrIndex = delAttrIndex;
	}

	public String getInsertType() {
		return insertType;
	}

	public void setInsertType(String insertType) {
		this.insertType = insertType;
	}

	public void addAttr() {

		for (XmlData obj : xmlObjList) {

			if (obj != null) {
				if (obj.getTagId().equals(targetTagId)) {
					obj.getAttrList().add(new Attribute());
					break;
				}
			}
		}
	}

	public void delAttr() {

		XmlData target = null;

		for (XmlData obj : xmlObjList) {

			if (obj != null) {
				if (obj.getTagId().equals(targetTagId)) {
					target = obj;
					break;
				}
			}
		}

		if (target != null) {

			target.getAttrList().remove(Integer.valueOf(delAttrIndex).intValue());

		}
	}

	public void delete() {

		int size = xmlObjList.size();

		for (int i = 0; i < size; i++) {

			XmlData obj  = xmlObjList.get(i);

			if (obj != null) {
				if (obj.getTagId().equals(targetTagId)) {
					xmlObjList.remove(i);
					break;
				}
			}
		}

		deleteChild(xmlObjList, targetTagId);

	}
	private void deleteChild(List<XmlData> targetList, String deleteTagId) {

		for (int i = 0; i < targetList.size(); i++) {

			XmlData obj  = targetList.get(i);

			if (obj != null) {
				if (obj.getOyaTagId().equals(deleteTagId)) {

					targetList.remove(i);
					deleteChild(targetList, obj.getTagId());
					--i;
				}
			}
		}
	}

	public void orderChange() {

		int targetIndex = 0;
		int changeIndex = 0;
		boolean find = false;
		String targetOyaId = null;

		for (XmlData obj : xmlObjList) {

			if (obj != null) {
				if (obj.getTagId().equals(targetTagId)) {
					targetOyaId = obj.getOyaTagId();
					break;
				}
			}

			++targetIndex;
		}

		for (XmlData obj : xmlObjList) {

			if (obj != null) {
				if (obj.getOyaTagId().equals(targetOyaId) && targetIndex < changeIndex) {
					find = true;
					break;
				}
			}

			++changeIndex;
		}

		if (find) {
			Collections.swap(xmlObjList, targetIndex, changeIndex);
		}

	}

	/**
	 *
	 * @return
	 */
	public XmlComposite xmlObjListToXmlDisObj() {

		displayTagId = xmlObjList.get(0).getTagId();

		return XmlOutputUtil.xmlObjListToXmlDisObj(xmlObjList);
	}


	public String getDisplayTagId() {
		return displayTagId;
	}

	public void setDisplayTagId(String displayTagId) {
		this.displayTagId = displayTagId;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

}
